<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Common extends Controller_Template {

    public $postfix;
    public $_config;
    public $sitetext;

	public function before() {
        parent::before();
        $this->_config = Kohana::$config->load('main');
        $this->sitetext = ORM::factory('Sitetext')->getAll($this->postfix);
        View::set_global('item_id', $this->request->param('id'));
        View::set_global('title', $this->sitetext['title']['_value']);
        View::set_global('description', $this->sitetext['meta_description']['_value']);
        View::set_global('sitetext', $this->sitetext);
        View::set_global('controller', $this->request->controller());
        $this->template->backgrounds = DB::select('image')->from('background')->order_by('sort', 'DESC')->as_object()->execute();
	}

    public function buildCarcas($submenu = '') {
        $this->template->header = View::factory('pieces/header', array(
            'menu' => ORM::factory('Menu')->getAll($this->postfix),
            'c_url' => $c_uri = Request::detect_uri(),
            'submenu' => $submenu
        ));

        $this->template->footer = View::factory('pieces/footer');
    }

    public function getOffset($per_page) {
        $currentPage = $this->request->param('page');

        if ($currentPage == 1 || empty($currentPage)) {
            return 0;
        } else {
            return $currentPage * $per_page - $per_page;
        }
    }

}
